using System.Xml.Serialization;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text;

namespace Client
{
	public class Music : IEquatable<Music>
	{
		private string m_Title;
		private string m_Path;
		private string m_Artist;
		private string m_Album;
		private string m_Key;
		private string m_Year;
		private bool m_IsSelected;
		private long m_Size;
		
		
		public string Title{
			get {return m_Title;}
			set {m_Title = value;}
		}
		public string Artist{
			get {return m_Artist;}
			set {m_Artist = value;}
		}
		public string Album{
			get {return m_Album;}
			set {m_Album = value;}
		}
		public string Year{
			get {return m_Year;}
			set {m_Year = value;}
		}
		public string Path{
			get {return m_Path;}
			set {m_Path = value;}
		}
		[XmlAttribute("Key")]
		public string Key{
			get {return m_Key;}
			set {m_Key = value;}
		}
		[XmlAttribute("Selected")]
		public bool IsSelected{
			get {return m_IsSelected;}
			set {m_IsSelected = value;}
		}
		[XmlAttribute("Size")]
		public long Size{
			get {return m_Size;}
			set {m_Size = value;}
		}
		
		public Music () {
			m_IsSelected = false;
		}
		
		// see : http://stackoverflow.com/questions/68283/view-edit-id3-data-for-mp3-files
		public Music (FileInfo fi) {
			m_IsSelected = false;
			
			using (FileStream fs = File.OpenRead(fi.FullName)) {
				if (fs.Length >= 128) {
				    MusicID3Tag tag = new MusicID3Tag();
				    fs.Seek(-128, SeekOrigin.End);
				    fs.Read(tag.TAGID, 0, tag.TAGID.Length);
				    fs.Read(tag.Title, 0, tag.Title.Length);
				    fs.Read(tag.Artist, 0, tag.Artist.Length);
				    fs.Read(tag.Album, 0, tag.Album.Length);
				    fs.Read(tag.Year, 0, tag.Year.Length);
				    fs.Read(tag.Comment, 0, tag.Comment.Length);
				    fs.Read(tag.Genre, 0, tag.Genre.Length);
				    string theTAGID = Encoding.Default.GetString(tag.TAGID);
				
				    if (theTAGID.Equals("TAG")) {
				        m_Title = ParseTag(tag.Title);
				        m_Artist = ParseTag(tag.Artist);
				        m_Album = ParseTag(tag.Album);
				        m_Year = ParseTag(tag.Year);
				        //string Comment = Encoding.Default.GetString(tag.Comment);
				        //string Genre = Encoding.Default.GetString(tag.Genre);
						
						
						if(m_Title == string.Empty)
							m_Title = fi.Name.Substring(0, fi.Name.Length - 4);
				    }
					else {
				        m_Title = fi.Name.Substring(0, fi.Name.Length - 4);
					    m_Album = "Unknow";
						m_Year = string.Empty;
					}
					
					m_Size = fi.Length;
					m_Path = fi.FullName;
					m_Key = Identifiant();
			    }
			}
		}
		
		public void SelectSwitched() {
			m_IsSelected = !m_IsSelected;
		}
		
		public bool Equals(Music other) {
			return (m_Title == other.Title && m_Album == other.Album);
		}
		
		private string Identifiant() {
		 long i = 1;
		 foreach (byte b in Guid.NewGuid().ToByteArray())
			i *= ((int)b + 1);
			
		 return string.Format("{0:x}", i - DateTime.Now.Ticks);	
		}
		
		private string ParseTag(byte[] b) {
			for(int i = 0; i < b.Length; i++) {
				if(b[i] == 0)
					b[i] = 32;
			}
			
			return Encoding.ASCII.GetString(b).Trim();
		}
	}
	
	
	public class MusicID3Tag {
	    public byte[] TAGID = new byte[3];
	    public byte[] Title = new byte[30];
	    public byte[] Artist = new byte[30];
	    public byte[] Album = new byte[30];
	    public byte[] Year = new byte[4];
	    public byte[] Comment = new byte[30];
	    public byte[] Genre = new byte[1];
	}

}

